<?php

class LoginController extends GeanyController {

    public function index(){ 
        return Redirect::to('login');
    }
	
    public function showLogin(){
        // show the form
        if ($this->isLogin()){ return Redirect::to('home'); }
        return View::make('shared/login');
    }

    public function doLogin(){
        // process the form
        // validate the info, create rules for the inputs
        $rules = array(
                'username'    => 'required|min:3', // make sure the email is an actual email
                'password' => 'required|alphaNum|min:3' // password can only be alphanumeric and has to be greater than 3 characters
        );

        // run the validation rules on the inputs from the form
        $validator = Validator::make(Input::all(), $rules);
        // if the validator fails, redirect back to the form
        if ($validator->fails()) {
                return Redirect::to('login')
                        ->withErrors($validator) // send back all errors to the login form
                        ->withInput(Input::except('password')); // send back the input (not the password) so that we can repopulate the form
        } else {
                // create our user data for the authentication
                $userdata = array(
                        'username' 	=> Input::get('username'),
                        'password' 	=> Input::get('password')
                );
        // attempt to do the login
        if (Auth::attempt($userdata)) {
            // validation successful!
        return Redirect::to('home');
            } else {	
                // validation not successful, send back to form	
                return Redirect::to('login')->withInput(Input::except('password')); // send back the input (not the password) so that we can repopulate the form;
            }
        }
    }
    
    public function doLogout(){
        Auth::logout(); // log the user out of our application
        return Redirect::to('login'); // redirect the user to the login screen
    }
    
    public function register(){
        // 1. setting validasi
        $validator = Validator::make(
            Input::all(),
            array(
                "username"                 => "required|unique:users,username",
                "password"              => "required|min:6",
                "password_confirmation" => "same:password",
            )
        );

        // 2a. jika semua validasi terpenuhi simpan ke database
        if($validator->passes())
        {
            $user = new User;
            $user->email    = strtolower(Input::get('email'));
            $user->password = Hash::make(Input::get('password'));
            $user->save();
            
            Auth::login($user);
            return Redirect::to("home");
        }
        // 2b. jika tidak, kembali ke halaman form registrasi
        else
        {
            return Redirect::to('register')
                ->withErrors($validator)
                ->withInput();
        }
    }
    
    public function showForbidden(){
        if (!$this->isLogin()){ return Redirect::to('login'); }
        return View::make('shared/forbidden');
    }
    
}
